<template>
  <!-- 用户主界面部分 -->
  <section class="user">
    <!-- 跳过按钮，点击后导航到主页 -->
    <button class="skip-button" @click="toHome()"></button>
    <!-- 主标题 -->
    <div class="main-top-title"></div>
    <!-- 用户选项容器 -->
    <div class="user_options-container">
      <!-- 用户选项文本部分 -->
      <div class="user_options-text">
        <!-- 未注册用户部分 -->
        <div class="user_options-unregistered">
          <h2 class="user_unregistered-title">没有账号？</h2>
          <!-- 注册按钮 -->
          <button class="user_unregistered-signup" id="signup-button" @click="signup">注册</button>
        </div>
        <!-- 已注册用户部分 -->
        <div class="user_options-registered">
          <h2 class="user_registered-title">已经注册？</h2>
          <!-- 登录按钮 -->
          <button class="user_registered-login" id="login-button" @click="login()">登陆</button>
        </div>
      </div>
      <!-- 表单容器，使用动态类绑定 -->
      <div :class="formClass" id="user_options-forms" class="input-box">
        <!-- 登录表单 -->
        <div class="user_forms-login">
          <h2 class="forms_title">登录</h2>
          <form class="forms_form">
            <fieldset class="forms_fieldset">
              <!-- 用户名输入框 -->
              <div class="forms_field">
                <input v-model="user.username" type="text" placeholder="默认:user" class="forms_field-input" required autofocus/>
              </div>
              <!-- 密码输入框 -->
              <div class="forms_field">
                <input v-model="user.password" type="password" placeholder="默认:user" class="forms_field-input" required/>
              </div>
            </fieldset>
            <!-- 登录表单按钮 -->
            <div class="forms_buttons">
              <!-- 取消注释以添加忘记密码功能 -->
              <!-- <button type="button" class="forms_buttons-forgot">忘记密码?</button> -->
              <!-- 登录按钮 -->
              <input type="submit" value="登录" @click="handleLogin()" class="forms_buttons-action">
            </div>
          </form>
        </div>
        <!-- 注册表单 -->
        <div class="user_forms-signup">
          <h2 class="forms_title">注册</h2>
          <form class="forms_form">
            <fieldset class="forms_fieldset">
              <!-- 用户名输入框 -->
              <div class="forms_field">
                <input type="text" v-model="user.username" placeholder="昵称" class="forms_field-input" required/>
              </div>
              <!-- 邮箱输入框 -->
              <div class="forms_field">
                <input type="email" v-model="user.email" placeholder="邮箱" class="forms_field-input" required/>
              </div>
              <!-- 密码输入框 -->
              <div class="forms_field">
                <input type="password" v-model="user.password" placeholder="密码" class="forms_field-input" required/>
              </div>
            </fieldset>
            <!-- 注册表单按钮 -->
            <div class="forms_buttons">
              <!-- 注册按钮 -->
              <input type="submit" value="注册" @click="handleRegister()" class="forms_buttons-action">
            </div>
          </form>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
import axios from 'axios';
import { useUserStore } from '../store/user';
import { useGlobalStore } from '../store/global';
import { ElMessage } from 'element-plus';

export default {
  setup() {
    const store = useUserStore();
    const Globalstore = useGlobalStore();
    return {
      Globalstore,
      store // 响应式数据
    };
  },
  data() {
    return {
      user: {
        username: "",
        password: "",
        nikename: "",
        email: "",
        name: "",
        sex: "",
        address: "",
        phone: ""
      },
      formClass: [
        "user_options-forms",
        "bounceRight"
      ]
    };
  },
  methods: {
    // 导航到主页的方法
    toHome() {
      this.$router.push("/")
          .then(() => {
            // 重新刷新页面
            location.reload();
          });
    },
    // 使用 Element Plus 显示提示信息的方法
    showTips(tips, type) {
      ElMessage({
        showClose: true,
        message: tips,
        type: type,
      });
    },
    // 切换到登录表单的方法
    login() {
      this.formClass.pop('bounceLeft');
      this.formClass.push('bounceRight');
    },
    // 切换到注册表单的方法
    signup() {
      this.formClass.pop('bounceRight');
      this.formClass.push('bounceLeft');
    },
    // 处理登录的方法
    handleLogin() {
      // 获取存储在 sessionStorage 中的用户数据
      const users = JSON.parse(sessionStorage.getItem('users')) || [];
      // 查找匹配的用户
      const foundUser = users.find(user => user.username === this.user.username && user.password === this.user.password);
      console.log(foundUser);
      if (foundUser) {
        // 将用户名存储在 sessionStorage 中
        sessionStorage.setItem('username', JSON.stringify(foundUser));
        this.showTips("登陆成功！", "success");
        // 可以进行一些跳转或其他操作
        this.$router.push("/")
            .then(() => {
              // 重新刷新页面
              location.reload();
            });
      } else {
        this.showTips("用户名和密码错啦！再想想吧！", "danger");
      }
      // 清空输入框
      this.user.username = '';
      this.user.password = '';
    },
    // 处理注册的方法
    handleRegister() {
      // 获取存储在 sessionStorage 中的用户数据
      const users = JSON.parse(sessionStorage.getItem('users')) || [];
      // 创建新用户对象
      const newUser = {
        username: this.user.username,
        password: this.user.password,
        name: this.user.name,
        sex: this.user.sex,
        address: this.user.address,
        phone: this.user.phone,
        email: this.user.email,
        nikename: this.user.nikename,
      };
      // 将新用户添加到用户列表中
      users.push(newUser);
      // 将用户列表存储在 sessionStorage 中
      sessionStorage.setItem('users', JSON.stringify(users));
      this.showTips("恭喜你成为我们的一份子！", "success");
      // 切换到登录表单
      this.formClass.pop('bounceLeft');
      this.formClass.push('bounceRight');
    }
  }
}
</script>
<style>
* {
  box-sizing: border-box;
}

body {
  font-family: "Montserrat", sans-serif;
  font-size: 12px;
  line-height: 1em;
}

.skip-button {
  background: no-repeat center/100% url(../src/assets/page/skip-button.png);
  position: absolute;
  top: 620px;
  width: 15%;
  height: 15%;
}

.main-top-title {
  position: absolute;
  top: 120px;
  width: 100%;
  height: 19px;
  background: no-repeat center/100% url(../src/assets/page/main-top-title.png);
}

button {
  background-color: transparent;
  padding: 0;
  border: 0;
  outline: 0;
  cursor: pointer;
}

input {
  background-color: transparent;
  padding: 0;
  border: 0;
  outline: 0;
}

input[type=submit] {
  cursor: pointer;
}

input::-moz-placeholder {
  font-size: 0.85rem;
  font-family: "Montserrat", sans-serif;
  font-weight: 300;
  letter-spacing: 0.1rem;
  color: #ccc;
}

input:-ms-input-placeholder {
  font-size: 0.85rem;
  font-family: "Montserrat", sans-serif;
  font-weight: 300;
  letter-spacing: 0.1rem;
  color: #ccc;
}

input::placeholder {
  font-size: 0.85rem;
  font-family: "Montserrat", sans-serif;
  font-weight: 300;
  letter-spacing: 0.1rem;
  color: #ccc;
}

@-webkit-keyframes bounceLeft {
  0% {
    transform: translate3d(100%, -50%, 0);
  }

  50% {
    transform: translate3d(-30px, -50%, 0);
  }

  100% {
    transform: translate3d(0, -50%, 0);
  }
}

@keyframes bounceLeft {
  0% {
    transform: translate3d(100%, -50%, 0);
  }

  50% {
    transform: translate3d(-30px, -50%, 0);
  }

  100% {
    transform: translate3d(0, -50%, 0);
  }
}

@-webkit-keyframes bounceRight {
  0% {
    transform: translate3d(0, -50%, 0);
  }

  50% {
    transform: translate3d(calc(100% + 30px), -50%, 0);
  }

  100% {
    transform: translate3d(100%, -50%, 0);
  }
}

@keyframes bounceRight {
  0% {
    transform: translate3d(0, -50%, 0);
  }

  50% {
    transform: translate3d(calc(100% + 30px), -50%, 0);
  }

  100% {
    transform: translate3d(100%, -50%, 0);
  }
}


@-webkit-keyframes showSignUp {
  100% {
    opacity: 1;
    visibility: visible;
    transform: translate3d(0, 0, 0);
  }
}

@keyframes showSignUp {
  100% {
    opacity: 1;
    visibility: visible;
    transform: translate3d(0, 0, 0);
  }
}

/**
 * * Page background
 * */
.user {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
  /*background: rgb(250, 250, 242);*/
  background: no-repeat center/100% url('../src/assets/page/main-bg.png');
  background-size: cover;
}

.user_options-container {
  position: relative;
  width: 80%;
}

.user_options-text {
  display: flex;
  justify-content: space-between;
  width: 100%;
  background-color: rgba(34, 34, 34, 0.85);
  border-radius: 3px;
}

/**
 * * Registered and Unregistered user box and text
 * */
.user_options-registered,
.user_options-unregistered {
  width: 50%;
  padding: 75px 45px;
  color: #fff;
  font-weight: 300;
}

.user_registered-title,
.user_unregistered-title {
  margin-bottom: 15px;
  font-size: 1.66rem;
  line-height: 1em;
}

.user_unregistered-text,
.user_registered-text {
  font-size: 0.83rem;
  line-height: 1.4em;
}

.user_registered-login,
.user_unregistered-signup {
  margin-top: 30px;
  border: 1px solid #ccc;
  border-radius: 3px;
  padding: 10px 30px;
  color: #fff;
  text-transform: uppercase;
  line-height: 1em;
  letter-spacing: 0.2rem;
  transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out;
}

.user_registered-login:hover,
.user_unregistered-signup:hover {
  color: rgba(34, 34, 34, 0.85);
  background-color: #ccc;
}

/**
 * * Login and signup forms
 * */

.input-box {
  background: no-repeat center/100% url('../src/assets/page/true.jpg');
}

.user_options-forms {
  position: absolute;
  top: 50%;
  left: 30px;
  width: calc(1124px / 1.9);
  height: calc(750px / 1.9);
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 2px 0 15px rgba(0, 0, 0, 0.25);
  overflow: hidden;
  transform: translate3d(100%, -50%, 0);
  transition: transform 0.4s ease-in-out;
}

.user_options-forms .user_forms-login {
  transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out;
}

.user_options-forms .forms_title {
  margin-bottom: 45px;
  font-size: 1.5rem;
  font-weight: 500;
  line-height: 1em;
  text-transform: uppercase;
  color: #B16C27;
  letter-spacing: 0.1rem;
}

.user_options-forms .forms_field:not(:last-of-type) {
  margin-bottom: 20px;
}


.user_options-forms .forms_field-input {
  width: 100%;
  border-bottom: 1px solid #ccc;
  padding: 6px 20px 6px 6px;
  font-family: "Montserrat", sans-serif;
  font-size: 1.2rem;
  font-weight: 300;
  color: #1a1a1a;
  letter-spacing: 0.1rem;
  transition: border-color 0.2s ease-in-out;
}

.user_options-forms .forms_field-input:focus {
  border-color: gray;
  font-size: 1.2rem;
}

.user_options-forms .forms_buttons {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 35px;
}

.user_options-forms .forms_buttons-forgot {
  font-family: "Montserrat", sans-serif;
  letter-spacing: 0.1rem;
  color: #ccc;
  text-decoration: underline;
  transition: color 0.2s ease-in-out;
}

.user_options-forms .forms_buttons-forgot:hover {
  color: #b3b3b3;
}

.user_options-forms .forms_buttons-action {
  background-color: #B16C27;
  border-radius: 3px;
  padding: 10px 35px;
  font-size: 1rem;
  font-family: "Montserrat", sans-serif;
  font-weight: 300;
  color: #fff;
  text-transform: uppercase;
  letter-spacing: 0.1rem;
  transition: background-color 0.2s ease-in-out;
}

.user_options-forms .forms_buttons-action:hover {
  background-color: #e58728;
}

.user_options-forms .user_forms-signup,
.user_options-forms .user_forms-login {
  position: absolute;
  top: 70px;
  left: 40px;
  width: calc(100% - 80px);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.4s ease-in-out, visibility 0.4s ease-in-out, transform 0.5s ease-in-out;
}

.user_options-forms .user_forms-signup {
  transform: translate3d(120px, 0, 0);
}

.user_options-forms .user_forms-signup .forms_buttons {
  justify-content: flex-end;
}

.user_options-forms .user_forms-login {
  transform: translate3d(0, 0, 0);
  opacity: 1;
  visibility: visible;
}

.user_options-forms.bounceLeft {
  -webkit-animation: bounceLeft 1s forwards;
  animation: bounceLeft 1s forwards;
}

.user_options-forms.bounceLeft .user_forms-signup {
  -webkit-animation: showSignUp 1s forwards;
  animation: showSignUp 1s forwards;
}

.user_options-forms.bounceLeft .user_forms-login {
  opacity: 0;
  visibility: hidden;
  transform: translate3d(-120px, 0, 0);
}

.user_options-forms.bounceRight {
  -webkit-animation: bounceRight 1s forwards;
  animation: bounceRight 1s forwards;
}

/**
 * * Responsive 990px
 * */
@media screen and (max-width: 990px) {
  .user_options-forms {
    min-height: 350px;
  }

  .user_options-forms .forms_buttons {
    flex-direction: column;
  }

  .user_options-forms .user_forms-login .forms_buttons-action {
    margin-top: 30px;
  }

  .user_options-forms .user_forms-signup,
  .user_options-forms .user_forms-login {
    top: 40px;
  }

  .user_options-registered,
  .user_options-unregistered {
    padding: 50px 45px;
  }
}

.forms_fieldset {
  border: none;
}
</style>